package com.woniu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.Result;
import com.woniu.mapper.CarMapper;
import com.woniu.mapper.GetSpaceStatusMapper;
import com.woniu.mapper.OrderMapper;
import com.woniu.mapper.RoadMapper;
import com.woniu.pojo.*;
import com.woniu.service.IGetSpaceStatusServices;
import com.woniu.vo.ChargeReturnVo;
import com.woniu.vo.GetSpaceStatusResVo;
import com.woniu.vo.UserOrderDetailsVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class GetSpaceStatusServicesImpl extends ServiceImpl<GetSpaceStatusMapper, Space> implements IGetSpaceStatusServices {
    @Resource
    private GetSpaceStatusMapper getSpaceStatusMapper;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private CarMapper carMapper;
    @Resource
    private RoadMapper roadMapper;
    @Resource
    private OrderSettlementServiceImpl orderSettlementService;

    @Override
    /**
     * 扫码或手动输入泊位号
     * */
    /**
     * 我拿到用户的id和车位号，我去看一下车位的状态有没有变成有车，正常是有车，否则是故障
     * 有车，我去判断一下这个订单是当前用户的吗？此时，我还没有帮他给车辆绑定！！
     * 如果它传过来了一个用户id，我就给他绑定上
     * */
    public Result getSpaceStatus(Long spaceId, Long userId) throws ParseException {
        //我有这个泊位id，就可以泊位的状态了
        Space space = getSpaceStatusMapper.selectById(spaceId);
        //拿到泊位了
        //判断一下状态
        if(space == null){
            //防止传过来的泊位id是个假的
            return new Result(false,"请输入正确的泊位号");
        }
        //有东西，判断一下有没有车,
        if(space.getStatus().equals("1")){
            //有车，还要去查正在进行的订单
            Order order = orderMapper.selectBySpaceId(spaceId);
            if(order != null){
                //已有车辆停放在泊位上且确认了订单
                Car car = carMapper.selectById(order.getCarId());
                //判断一下是不是刚生成的车辆
                if(!car.getUserId().toString().equals("0")){
                    return new Result(false,"该车位已被占用，请输入正确的泊位");
                }
            }
        }
        //泊位空闲，且正确
        //那我吧之前创建的订单查出来，返回去
        if(space.getStatus().equals("0")){
            //我吧车开进去了，但是状态没改，
            return new Result(false,"未检测到车辆,请重新驶入");
        }
        Order order = orderMapper.selectBySpaceId(spaceId);
        if(order == null){
            return new Result(false,"查无此订单");
        }
        //改一下车辆的用户,给他关联上
        carMapper.updateCarUserById(order.getCarId(),userId);
        //查出来了订单了，再查一个收费信息。
        List<ChargeReturnVo> voList = orderMapper.searchChargeRule(space.getRoadId());
        //吧查出来的信息封装返回去
        GetSpaceStatusResVo vo = new GetSpaceStatusResVo();
        vo.setList(voList);
        //设置驶入时间
        vo.setCreateTime(order.getCreateTime());
        vo.setId(order.getId());
        //设置订单车牌
        Car car = carMapper.selectById(order.getCarId());
        vo.setCarNum(car.getNum());
        //设置订单的路段民
        String road = getSpaceStatusMapper.selectRoadBySpaceId(order.getSpaceId());
        vo.setRoadName(road);
        //设置停车时长
        long time = order.getCreateTime().getTime();
        long now = new Date().getTime();
        //这个是分钟
        Long parkingTime = (now-time)/1000/60;
        //我看看你听了多少天
        Long day =  parkingTime/60/24;
        //听了几个小时
        Long hour = (parkingTime-day*24*60)/60;
        //听了几分钟
        Long mm = (parkingTime-day*24*60-hour*60);
        vo.setParkingTime(day.toString()+"天"+hour+"小时"+mm+"分");
        //设置订单金额
        //计费规则已经有了，
        Result result1 = orderSettlementService.currentCost(order.getId());
        vo.setParkingMoney(result1.getData().toString());
        Result result = new Result(true,"操作成功",vo);
        return result;
    }

    @Override
    /**
    *
    * 确认订单并把订单详情返回
    * */
    public Result getOrder(Long userId, Long orderId) {
        //先查订单
        Order order = orderMapper.selectById(orderId);
        //如果找不到订单的话
        if(order == null){
            return new Result(false,"查无此订单");
        }
        UserOrderDetailsVo orderDetails = new UserOrderDetailsVo();
        //设置创建时间
        orderDetails.setCreateTime(order.getCreateTime());
        //设置订单表号
        orderDetails.setId(order.getId());
        //设置订单的泊位好
        orderDetails.setSpaceId(order.getSpaceId());
        Car car = carMapper.selectById(order.getCarId());
        //设置订单的车牌
        orderDetails.setCarNum(car.getNum());
        //设置订单的听策划路段
        String roadName =  getSpaceStatusMapper.selectRoadBySpaceId(order.getSpaceId());
        orderDetails.setRoadName(roadName);
        //设置订单的图片集合
        List<String> list =  getSpaceStatusMapper.selectImageListByOrderId(orderId);
        orderDetails.setList(list);
        //设置停车的时间
        long time = order.getCreateTime().getTime();
        long now = new Date().getTime();
        //这个是分钟
        Long parkingTime = (now-time)/1000/60;
        //我看看你听了多少天
        Long day =  parkingTime/60/24;
        //听了几个小时
        Long hour = (parkingTime-day*24*60)/60;
        //听了几分钟
        Long mm = (parkingTime-day*24*60-hour*60);
        orderDetails.setParkingTime(day.toString()+"天"+hour+"小时"+mm+"分");

        return new Result(true,"查询成功",orderDetails);
    }

    public BigDecimal findCurrentMoney(Long roadId,Date createTime){
        //先查收费模式
        List<ChargeReturnVo> voList = orderMapper.searchChargeRule(roadId);
        //看看今天是工作日还是非工作日
        Calendar instance = Calendar.getInstance();
        instance.setTime(new Date());

        return null;
    }
}
